<template>
  <a-layout id="components-layout-demo-custom-trigger">
    <a-layout-sider v-model="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu theme="dark" mode="inline" :default-selected-keys="['0']">
        <a-menu-item key="0">
          <router-link to="/">
            <a-icon type="home" />
            <span>首页</span>
          </router-link>
        </a-menu-item>
        <template v-for="(menu,index) in menuList">
          <a-sub-menu v-if="menu.children" :key="menu.id" :index="index">
            <span slot="title">
              <template v-if="menu.icon">
                <a-icon :type="menu.icon" />
              </template>
              
              <span v-text="menu.name"></span>
            </span>
            <a-menu-item v-for="(item,index) in menu.children" :key="item.id" :index="index">
              <router-link :to="item.url">
                <template v-if="item.icon">
                <a-icon :type="item.icon" />
                </template>
              
                <span v-text="item.name"></span>
              </router-link>
            </a-menu-item>
          </a-sub-menu>
          <a-menu-item v-else :key="menu.id" :index="index">
            <router-link :to="menu.url">
              <a-icon :type="menu.icon" />
              <span v-text="menu.name"></span>
            </router-link>
          </a-menu-item>
        </template>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <a-icon
          class="trigger"
          :type="collapsed ? 'menu-unfold' : 'menu-fold'"
          @click="() => (collapsed = !collapsed)"
        />

        <a-dropdown class="user-info">
          <span>
            <a-avatar size="small" class="avatar" :src="baseURL+userInfo.avatar"/>
            <span v-text="userInfo.name"></span>
          </span>
          <a-menu slot="overlay">
            <a-menu-item key="0">
              <a @click="logout()">
                <a-icon type="logout" />
                <span>退出登录</span>
              </a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>


      </a-layout-header>
      <a-layout-content
        :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: 'auto'}"
      >
        <router-view></router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
<script>
import { mapState ,mapActions} from "vuex";
import { treeDataTranslate,baseURL } from "@/util/index";
export default {
  data() {
    return {
      collapsed: false,
      baseURL
    };
  },
 
  computed: {
    ...mapState("authorization", ["resources","userInfo"]),
    menuList() {
      return treeDataTranslate(
        this.resources.filter(resource => resource.type < 2)
      );
    }
  },
  methods:{
    ...mapActions("authorization", ["LOGOUT"]),
    logout(){
      this.LOGOUT().then(response => {
              this.$router.replace('login')
            })
            .catch(error => {
              this.$message.error(error);
            });
    }
  }
};
</script>
<style>
#components-layout-demo-custom-trigger .user-info {
  position: absolute;
  right: 45px;
}
#components-layout-demo-custom-trigger .user-info .avatar {
  margin: 20px 8px 20px 0;
}

#components-layout-demo-custom-trigger {
  height: 100vh;
}

#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}

</style>
